/**
 * Collection lists
 * -- community
 * -- community_bulletins
 * -- community_supplements
 */

var Schema = mongoose.Schema;

//==================================================================================
var communityMongoSchema = new Schema({
    communityType: Number,                                               //0,虚拟小区 1,县级小区
    communityName: String,                                                  //小区名称
    communityName_en_US: String,                                            //小区名称英文
    pinyin: String,                                                         //小区名称拼音
    communityLogo: String,                                                     //小区logo
    cityId: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},       //小区所属市id
    countyId: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},     //小区所属县
    provinceId: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},   //小区所属省
    city: String,                                                           //小区所属市
    populationSize: Number,                                                   //人口规模
    communityAddress: String,                                                //小区地址
    communityPostCode: String,                                               //邮政编码
    isValid: {type: Boolean, default: false},
    households: Number,
    chatGroupId: String,
    location: {
        longitude: Number,
        latitude: Number
    },
    phoneList: [{
        name: String,
        phoneNo: String,
        address: String,
        user: {type: Schema.Types.ObjectId, ref: 'user'}
    }],
    groups: [
        {type: Schema.Types.ObjectId, ref: 'community_group'}
    ],
    unitSwitch: {type: Boolean, default: false},
    price: Number,
    dateBuild: Date,
    isInit: {type: Boolean, default: false},    //是否已初始化
    lastModifiedBy: {type: Schema.Types.ObjectId, ref: 'user'},
    dateEffective: {type: Date},
    dateLastModified: {type: Date, default: Date.now},
    versionNo: String
});
communityMongoSchema.index({'location': '2dsphere'});
mongoose.model('community', communityMongoSchema, 'community');

/*  宝宝树66小区二次改造    add   小区切换 luolh  20170803   start   */
var abmCommunityMongoSchema = new Schema({
    community: {type: Schema.Types.ObjectId, ref: 'community'},               // 所属小区
    user: {type: Schema.Types.ObjectId, ref: 'user'},                           // 创始人
    isValid: {type: Boolean, default: false},                                     // 是否有效
    dateCreated: {type: Date, default: Date.now},                                 //  创建时间
    dateLastModified: {type: Date, default: Date.now}                            //  最后修正时间
});
mongoose.model('ambassador_community', abmCommunityMongoSchema, 'ambassador_community');
/*  宝宝树66小区二次改造    add   小区切换 luolh  20170803   start   */

var communityGroupSchema = new Schema({
    groupName: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('community_group', communityGroupSchema, 'community_group');


//==============================community_bulletins=====================================
var bulletinMongoSchema = new Schema({
    community: [
        {type: Schema.Types.ObjectId, ref: 'community'}
    ],
    title: String,
    imageURL: String,                                     //封面
    isFresh: {type: Boolean, default: false},
    desc: String,
    isValid: {type: Boolean, default: false},
    dateCreated: {type: Date, default: Date.now},
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'}
});
mongoose.model('community_bulletins', bulletinMongoSchema, 'community_bulletins');

//1.5.0
var bannerSchema = new Schema({
    contentId: String,
    openType: Number,
    contentCreatedBy: String,
    contentUrl: String,
    imageURL: String,
    tag: String,                                         //标签
    bgColor: String,                                     //标签颜色
    message: String,
    type: Number,                                       //1,banner;2,新鲜事
    isValid: {type: Boolean, default: true},
    dateLastModified: {type: Date, default: Date.now},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('community_banner', bannerSchema, 'community_banner');

var bannerBindSchema = new Schema({
    bannerId: {type: Schema.Types.ObjectId, ref: 'community_banner'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    type: Number,                                        //1,banner;2,新鲜事
    dateCreated: {type: Date, default: Date.now},
    displayOrder: {type: Number, default: 0}
});
mongoose.model('community_banner_bind', bannerBindSchema, 'community_banner_bind');

//=============================community_supplements=======================================
var communitySupplementSchema = new Schema({
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    id: String,
    desc: String,
    phone: String,
    address: String,
    dateCreated: {type: Date, default: Date.now},
    status: {type: Boolean},
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'}
});
mongoose.model('community_supplements', communitySupplementSchema, 'community_supplements');

var communityApplicationSchema = new Schema({
    city: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    county: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    communityName: String,
    address: String,
    note: String,
    type: {type: Number, default: 1},                       // 1,自己小区 2,父母小区
    status: {type: Number, default: 0},
    isValid: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('community_applications', communityApplicationSchema, 'community_applications');

var communityCitySchema = new Schema({
    cityId: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    name: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('community_city', communityCitySchema, 'community_city');

var locationVSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: String,
    lat: Number,
    lng: Number,
    message: String,
    desc: String,
    distance: Number,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('community_location_v', locationVSchema, 'community_location_v');

var weekReportSchema = new Schema({
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    process_date: {type: Date, default: Date.now},
    new_neighbors: [{
        uid: {type: Schema.Types.ObjectId, ref: 'user'},
        rank: Number
    }],
    contribution: [{
        uid: {type: Schema.Types.ObjectId, ref: 'user'},
        rank: Number
    }],
    hotlist: [{
        qid: {type: Schema.Types.ObjectId, ref: 'question'},
        rid: {type: Schema.Types.ObjectId},
        rank: Number,
        browserCount: Number,
        replyCount: Number
    }]
});
mongoose.model('weekly_report', weekReportSchema, 'weekly_report');

var weekScoreSchema = new Schema({
    userId: {type: Schema.Types.ObjectId, ref: 'user'},
    adopt: Number,
    thank: Number,
    praise: Number,
    addRecord: Number,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('user_week_score', weekScoreSchema, 'user_week_score');

var cardSchema = new Schema({
    contentId: String,
    openType: Number,
    title: String,
    desc: String,
    imageURL: String,                                    //图片
    url: String,                                         //url
    level: Number,                                       //0:全站 1:省 2:市 3:县区 4:小区
    province: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    city: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    county: {type: Schema.Types.ObjectId, ref: 'masterData_Regions'},
    beginDate: {type: Date},                            //开始日期
    endDate: {type: Date},                              //结束日期
    isValid: {type: Boolean, default: true},
    dateLastModified: {type: Date, default: Date.now},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('card', cardSchema, 'card');

var cardSchema = new Schema({
    contentId: String,
    openType: Number,
    contentCreatedBy: String,
    contentUrl: String,
    title: String,
    desc: String,
    imageURL: String,
    type: Number,                                       //1,专题
    isValid: {type: Boolean, default: true},
    beginTime: {type: Date},
    endTime: {type: Date},
    displayOrder: {type: Number, default: 0},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('community_card', cardSchema, 'community_card');
